package com.yyg.usecase.concurrent;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * @Author: 冰岩
 * @Date: 2024/03/27
 * @Description:
 */
public class ScheduledExecutorServiceTest {

    @Test
    public void scheduleAtFixedRateTest() throws InterruptedException {
        ThreadFactory threadFactory = new NumberedThreadFactory("scheduleAtFixedRate");
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(2, threadFactory,
            new ThreadPoolExecutor.AbortPolicy());
        Runnable task1 = () -> {
            System.out.println(Thread.currentThread().getName() + " hello world 1");
        };
        Runnable task2 = () -> {
            System.out.println(Thread.currentThread().getName() + " hello world 2");
        };
        executorService.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS);
        executorService.scheduleAtFixedRate(task2, 0, 1, TimeUnit.SECONDS);
        executorService.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS);
        Thread.sleep(5000);
        executorService.shutdown();
    }

}
